<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #result {
            width: 200px;
            height: 100px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <h1>同源策略</h1>
    <button>获取用户数据</button>
    <div id="result"></div>
    <script>
        const btn = document.querySelector("button");
        const result = document.querySelector("#result");

        btn.onclick = function() {
            const xhr = new XMLHttpRequest();
            // 因为是同源策略，所以url可以简写
            xhr.open("GET", "/data");
            xhr.send();
            xhr.onreadystatechange = function() {
                if(xhr.readyState === 4) {
                    if(xhr.status >= 200 && xhr.status <300) {
                        console.log(xhr.status);
                        console.log(xhr.response);
                    }
                }
            }
        };




        
    </script>
</body>
</html>